package com.qf.web.servlet;

import com.qf.domain.Cartltem;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.impl.CartServiceimpl;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//购物车
@WebServlet(value = "/cartservlet")
public class CartServlet extends BaseServlet {
    //把商品添加到购物车
    public String addCart(HttpServletRequest request, HttpServletResponse response){

        //首先要先获取穿过来的商品id 和数量
        User user = (User) request.getSession().getAttribute("user");
        if (user==null){
            request.setAttribute("msg","请先登陆再添加购物车");
            return "/login.jsp";
        }
        //根据当前用户的id查询自己购物车里面是否已经添加过这个商品  也就是查看购物车里面用户id所对应的goodsid
        String goodsld = request.getParameter("goodsId");
        String number = request.getParameter("number");
        Integer number1=Integer.parseInt(number);
        if (number1<1){
            number1=1;
        }
        try {
            //获取商品信息
            GoodsService goodsService=new GoodsServiceImpl();
            Goods byid = goodsService.findByid(Integer.parseInt(goodsld));
            if (byid==null){
                request.setAttribute("msg","您添加的商品已经下架，亲，商品不存在");
                return "/message.jsp";
            }
            if (byid!=null){
                //获取购物车信息
                CartService cartService=new CartServiceimpl();
                Cartltem cartltem=cartService.selectUserCart(user.getId(),Integer.parseInt(goodsld));
                System.out.println("我来到这里");
                if (cartltem!=null){
                    //如果商品里面有这个商品 那么我们需要获取购物车里面的这个用户id所对应的这个商品的所有信息，然后修改，让这个用户id里面的数量和价格调整
                    //修改购物车信息
                    cartService.UpdateCartGoods(user.getId(),Integer.parseInt(goodsld),byid.getPrice().add(cartltem.getMoney()),cartltem.getNum()+number1);
                }else {
                    //如果查询之后没有 那么我们要先查询这个商品的信息，然后获取商品的价格，我门再根据当前登陆的用户来添加购物车商品
                    cartService.InsertUserCart(user.getId(),Integer.parseInt(goodsld),number1,byid.getPrice());
                }
            }
            return "/cartSuccess.jsp";
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("msg","添加失败");
            return "/message.jsp";
        }
    }
    //查看购物车
    public String getCart(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        if (user==null){
            request.setAttribute("msg","请先登陆才能查看购物车");
            return "/login.jsp";
        }else {
            Integer id = user.getId();
            CartService cartService=new CartServiceimpl();
            List<Cartltem> cartItems=cartService.selectUserCartAll(id);
            if (cartItems==null){
                request.setAttribute("msg","您的购物车没有商品，请先添加商品");
                return "/message.jsp";
            }else {
                request.setAttribute("cartItems",cartItems);
                return "/cart.jsp";
            }
        }
        //判断是否登陆
        //创建业务对象
        //调用findcart
    }
    //ajax刷新局部添加删除请求
    public String addCartAjax(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("user");
        System.out.println("走这了");
        if (user==null){
            request.setAttribute("msg","请先登陆才能查看购物车");
            return "/login.jsp";
        }else {
            String goodsId = request.getParameter("goodsId");
            String number = request.getParameter("number");
            Integer number1 = Integer.parseInt(number);
            System.out.println(number1);
            if (StringUtils.empty(goodsId)){
                request.setAttribute("msg","操作失效");
                return "/message.jsp";
            }
            if (StringUtils.empty(number)){
                request.setAttribute("msg","操作失效");
                return "/message.jsp";
            }
            //查询用户的购物车
            CartService cartService=new CartServiceimpl();

            Cartltem cartltem = cartService.selectUserCart(user.getId(), Integer.parseInt(goodsId));
            if (number1!=0) {
                //更新
                cartltem.setNum(cartltem.getNum()+number1);
                if (number1<0){
                    //获取商品信息
                    GoodsService goodsService=new GoodsServiceImpl();
                    Goods byid = goodsService.findByid(cartltem.getPid());
                    cartltem.setMoney(cartltem.getMoney().subtract(byid.getPrice()));
                    cartService.UpdateCartGoods(user.getId(),Integer.parseInt(goodsId),cartltem.getMoney(),cartltem.getNum());
                }else {
                    GoodsService goodsService=new GoodsServiceImpl();
                    Goods byid = goodsService.findByid(cartltem.getPid());
                    cartltem.setMoney(cartltem.getMoney().add(byid.getPrice()));
                    cartService.UpdateCartGoods(user.getId(),Integer.parseInt(goodsId),cartltem.getMoney(),cartltem.getNum());
                }
            }else {
                cartService.deleteUserCart(user.getId(),Integer.parseInt(goodsId));
            }
        }
        return null;
    }
}
